<?php
require_once "header_editor.php";
require_once "util.php";
require_once "forms.php";
require_once "db.php";

get_database_connection();

$TMP_PREFIX = "tmp/";
$SKEL_PREFIX = "skel/";

// Checking if the tmp folder exists, if not, creates.
if (!@is_dir($TMP_PREFIX)) {
  @mkdir($TMP_PREFIX);
}

// Getting the current object
$object_id = $_SESSION["object_id"];
$object_dir = $TMP_PREFIX . $object_id;

// Generating the SCORM object FOLDER.
@mkdir($object_dir);

// Coping the skel files
copy($SKEL_PREFIX . "adlcp_rootv1p2.xsd", $object_dir . "/adlcp_rootv1p2.xsd");
copy($SKEL_PREFIX . "APIWrapper.js", $object_dir . "/APIWrapper.js");
copy($SKEL_PREFIX . "ims_xml.xsd", $object_dir . "/ims_xml.xsd");
copy($SKEL_PREFIX . "imscp_rootv1p1p2.xsd", $object_dir . "/imscp_rootv1p1p2.xsd");
copy($SKEL_PREFIX . "imsmd_rootv1p2p1.xsd", $object_dir . "/imsmd_rootv1p2p1.xsd");
copy($SKEL_PREFIX . "imsmanifest.xml", $object_dir . "/imsmanifest.xml");

//
// Getting the data to be used based on the object_id
//

// Object data
$sql = "SELECT * FROM objects WHERE id = $object_id";
$object_query = mysql_query($sql);
$object_data = mysql_fetch_assoc($object_query);

// Slides
$sql = "SELECT * FROM slides WHERE object_id = $object_id ORDER BY position ASC";
$slides_query = mysql_query($sql);

// Opening the imsmanifest.xml to replace info.
$imsmanifest_path = $object_dir . "/imsmanifest.xml";

// Getting content
$fp = fopen($imsmanifest_path, "r");
$imsmanifest_content = fread($fp, filesize($imsmanifest_path));
fclose($fp);

// Modifing the imsmanifest.xml TITLE.
$imsmanifest_content = str_ireplace("###OBJECT_TITLE###", $object_data["title"], $imsmanifest_content);


// Generating the pages contents.
$organizations = array();
$resources = array();

while ($slide = mysql_fetch_assoc($slides_query)) {
  $id = $slide["id"];
  $title = $slide["title"];
  $content = $slide["content"];

  $organization = "
  <item identifier=\"o$id\" isvisible=\"true\" identifierref=\"r$id\">
        <title>$title</title>
      </item>
  ";
  array_push($organizations, $organization);

  $resource = "
    <resource 
    xml:base=\"\" 
    href=\"r$id.html\" 
    identifier=\"r$id\" adlcp:scormtype=\"sco\" type=\"webcontent\">
      <file href=\"$id.html\" />
    </resource>  
  ";
  array_push($resources, $resource);

  // Writing the HTML file.
  $html = "
  <!doctype html>
  <html>
  <head>
    <script type='text/javascript' src='APIWrapper.js'></script>
  </head>
  <body>
    $content
  </body>
  </html>
  ";
  
  // Removing the base_url. 
  $html = str_replace($_SESSION["base_url"], "/", $html);

  $slide_fp = fopen($object_dir . "/r" . $id . ".html", "w");
  fwrite($slide_fp, $html);
  fclose($slide_fp);
}

// Imploding the array and replacing in the file.
$organizations = implode("\n", $organizations);
$resources = implode("\n", $resources);

$imsmanifest_content = str_ireplace("###ORGANIZATIONS###", $organizations, $imsmanifest_content);
$imsmanifest_content = str_ireplace("###RESOURCES###", $resources, $imsmanifest_content);

// Overwriting the file.
$fp = fopen($imsmanifest_path, "w");
fwrite($fp, $imsmanifest_content);
fclose($fp);


// Generating the ZIP File
$zip_destination = $TMP_PREFIX . "/" . $object_id . ".zip";

$zip = new ZipArchive();
$zip->open($zip_destination, ZIPARCHIVE::CREATE);

// Adding base files
$files_to_be_ziped = scandir($object_dir);
foreach($files_to_be_ziped as $f) {
  $real_path_f = $object_dir . "/" . $f;

  if (is_file($real_path_f)) {
    $zip->addFile($real_path_f, $f);
  }
}

$zip->close();
?>

<div class="c6"> <br/><br/> </div>

<div class="c6"> <h1> Gerando arquivo ZIP <img src="images/loading.gif"></img> </h1> </div>

<script type="text/javascript">
function redirect_to_zip() {
  ddd_redirect("./<?php echo $zip_destination ?>");
}

setTimeout(redirect_to_zip, 2500);
</script>

<?php
require_once "footer.php";
?>
